package com.benchmark.springboot.controller;

import com.power.common.model.CommonResult;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.LivenessState;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * SpringBoot 2.3+存活探针测试
 *
 * @author yu 2020/9/22.
 */
@RestController
@RequestMapping("liveness")
public class LivenessSettingController {

    @Resource
    private ApplicationEventPublisher eventPublisher;


    /**
     * 设置活探针为可用状态
     * @return
     */
    @GetMapping("/correct")
    public CommonResult<String> markLivenessCorrect() {
        AvailabilityChangeEvent.publish(eventPublisher, this, LivenessState.CORRECT);
        return CommonResult.ok().setResult("Liveness marked as CORRECT");
    }

    /**
     * 设置存活探针设置为不可用状态
     * @return
     */
    @GetMapping("/broken")
    public CommonResult<String> markLivenessBroken() {
        AvailabilityChangeEvent.publish(eventPublisher, this, LivenessState.BROKEN);
        return CommonResult.ok().setResult("Liveness marked as BROKEN");
    }
}
